define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mox/inherit/policy/index' + location.search,
                    add_url: 'mox/inherit/policy/add',
                    edit_url: 'mox/inherit/policy/edit',
                    del_url: 'mox/inherit/policy/del',
                    multi_url: 'mox/inherit/policy/multi',
                    import_url: 'mox/inherit/policy/import',
                    table: 'mox_inherit_policy',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'release_time', title: __('Release_time'), sortable: true, formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange' },
                        // { field: 'content', title: __('Content'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        {
                            field: 'attachment',
                            title: __('附件'),
                            operate: false,
                            formatter: function (value, row, index) {
                                if (!row.attachmentfilename || row.attachmentfilename == 'null' || row.attachmentfilename == '') {
                                    return '<span class="text-muted">无附件</span>';
                                } else {
                                    console.log('Attachment URL:', value);
                                    // 安全过滤
                                    const safeUrl = value.replace(/[<>"']/g, '');
                                    const filename = row.attachmentfilename.filename;
                                    const ext = filename.split('.').pop().toLowerCase();

                                    // 仅处理PDF文件
                                    if (ext !== 'pdf') {
                                        return `<span class="text-danger">非PDF文件</span>`;
                                    }

                                    // PDF文件显示
                                    return `
                                <div class="pdf-attachment">=
                                <i class="fa fa-file-pdf-o text-danger"></i>
                                <a href="${safeUrl}" 
                                target="_blank" 
                                class="pdf-download"
                                title="点击下载PDF"
                                download="${filename}">
                                ${filename}
                                </a>
                                ${row.pdf_thumb ? `<img src="${row.pdf_thumb}" class="pdf-thumbnail">` : ''}
                                 <button class="btn btn-xs btn-default pdf-preview" 
                                  data-url="${safeUrl}"
                                  title="在线预览">
                                  <i class="fa fa-eye"></i>
                                   </button>
                                   </div>
                                   `;
                                }
                            },
                            events: {
                                'click .pdf-preview': function (e, value, row, index) {
                                    // 使用PDF.js或第三方服务预览
                                    window.open(value, '_blank');
                                }
                            }
                        },
                        { field: 'user.nickname', title: __('关联用户') },
                        { field: 'create_time', title: __('Create_time'), sortable: true, formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange' },
                        { field: 'update_time', title: __('Update_time'), sortable: true, formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange' },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
